<template>
	<view>
		<u-navbar title="我的爱车"></u-navbar>
		<!-- {{formdata}} -->
		<u--form labelPosition="left" ref="uForm" :model="formdata" :rules="rules">
			<u-form-item label="车牌号" labelPosition="top" prop="carnum">
				<view style="flex: 1">
					<keyboard-plate ref="plateNumber" :carnum.sync="formdata.carnum" isShow></keyboard-plate>
				</view>
			</u-form-item>
			<qcpp :valueqcpp.sync="formdata.qcpp" :valuecx.sync="formdata.cx" :valuejtpz.sync="formdata.jtpz"></qcpp>
			<vincode v-model="formdata.vincode"></vincode>

		</u--form>

		<fixedbtn v-if="!query.id">
			<u-button type="primary" size="medium" @click="editfn">{{$z.$t("确定")}}</u-button>
		</fixedbtn>

		<fixedbtn v-else>
			<view style="margin: 0 20rpx;flex: 1;">
				<u-button type="primary" size="medium" @click="editfn">{{$z.$t("确定")}}</u-button>
			</view>
			<view style="margin: 0 20rpx;flex: 1;">
				<u-button type="error" size="medium" @click="delfn">{{$z.$t("删除")}}</u-button>
			</view>
		</fixedbtn>
		<zmodal ref="zmodal"></zmodal>
	</view>
</template>

<script>
	import vincode from "./components/vincode.vue";
	import qcpp from "./components/qcpp.vue";
	export default {
		components: {
			qcpp,
			vincode,
		},
		data() {
			return {
				popupShow: true,
				list: [],
				formdata: {
					carnum: "",
					qcpp: "",
					cx: "",
					jtpz: "",
					vincode: "", //vin码
					lxdh: "",
				},
				rules: {
					carnum: [{
						required: true,
						message: this.$z.$t("请输入车牌号"),
						trigger: "change",
					}, ],
				},
				query: {},
				CarmanageinfoSearch: {}
				// vincode	输入	vin码
			};
		},

		onLoad(query) {
			this.query = query;
			this.init();
		},
		methods: {
			async init() {
				await this.$store.dispatch("user/getuser");
				if (this.query.id) {
					let CarmanageinfoSearch = await this.$z.request("/CarmanageinfoSearch", {
						id: this.query.id
					});
					if (CarmanageinfoSearch.length == 1) {
						this.formdata = CarmanageinfoSearch[0]
						this.CarmanageinfoSearch = {
							...CarmanageinfoSearch[0]
						}
					}
					console.log(CarmanageinfoSearch, "CarmanageinfoSearch");
				}
			},
			async delfn() {
				this.$refs.zmodal.init({
					content: this.$z.$t("删除此数据？"),
					showcancel: true,
					confirm: async () => {
						await uni.$z.request("/CarmanageinfoDel", {
							id: this.query.id
						});
						if (this.$store.state.user.USearch.ccp == this.CarmanageinfoSearch.carnum) {
							await this.setccp("")
						}
						this.$z.back();
					},
				});
			},
			async setccp(ccp) {
				let par = await uni.$z.request("/Uedtext", {
					b_bxr: this.$store.state.user.USearch.b_bxr || "0",
					ccp: ccp
				});

				if (par[0].par == "succeed") {
					await this.$store.dispatch("user/getuser", true);
					// this.$z.back();
					this.$u.toast(this.$z.$t("操作成功"));
				} else {
					let str = config.pars[par[0].par] || ""
					this.$u.toast(str)
				}
			},
			async editfn() {
				await this.$refs.uForm.validate();

				let reqdata = {
					...this.formdata,
					caruser: this.$store.state.user.USearch.uUser,
					lxdh: this.$store.state.user.USearch.utel,
				};
				console.log(reqdata);
				let url = this.query.id ? "/CarmanageinfoEdt" : "/CarmanageinfoAdd"
				await this.$z.request(url, reqdata);
				this.$refs.zmodal.init({
					content: this.$z.$t("操作成功"),
					confirm: async () => {
						await this.setccp(reqdata.carnum)
						// try {
						// 	let pages = getCurrentPages();
						// 	let prevPage = pages[pages.length - 2]; // 上一个页面

						// 	prevPage.init()
						// } catch {

						// }
						this.$z.back();
					},
				});
			},
			// 打开车牌选择器
			carInputClick() {
				this.$refs.plateNumber.open();
			},
		},
	};
</script>

<style></style>